#!/bin/bash

echo -e "\n==> EXECUTING @$(date -u +'%Y-%m-%d_%H-%M-%S'): ${0} $@\n"

declare _mydir=$(dirname $0)
source "${_mydir}"/env.rc
source "${_mydir}"/util.rc

deployment_push() {
    local target
    local repo="${_deploy_repo##*/}"
    local keeper_repo_tag="${_deploy_builder_tags[0]}"
    local -i exit_code=0

    if [[ -n "${repo}" && "${repo}" != "${_prohibited_repo_name}" ]] ; then

        ### push all target tags into the deployment repository
        for t in "${_deploy_tags[@]}" ; do

            ### note that the builder and deployment repositories could be identical
            ### in that case skip the tag which has been already published above
            if [[ "${DOCKER_REPO}" != "${_deploy_repo}" || "${keeper_repo_tag}" != "${t}" ]] ; then

                target="${_deploy_repo}:${t}"

                echo ; echo "Deploying image '${target}'"
                docker tag "${DOCKER_REPO}:${keeper_repo_tag}" "${target}"
                docker push "${target}"
                exit_code=$?
                docker rmi "${target}"

                if [[ ${exit_code} -ne 0 ]] ; then
                    docker logout
                    die "Unable to push image '${target}'" ${exit_code}
                fi
            fi
        done

    else
        echo "Skipping deployment repo push because of null or prohibited deployment repository '${repo}'."
    fi
}

main() {
    local target
    local repo="${DOCKER_REPO##*/}"
    local keeper_repo_tag="${_deploy_builder_tags[0]}"
    local -i exit_code=0

    if [[ -f "${_build_context}/${_scrap_demand_stop_building}" ]] ; then
        echo "Skipping push on demand."
        return 0
    fi

    ### Docker Hub log-in
    if [[ -n "${DOCKERHUB_USERNAME}" && -n "${DOCKERHUB_PASSWORD}" ]] ; then

        echo "Logging-in on Docker Hub"
        echo "${DOCKERHUB_PASSWORD}" | docker login -u "${DOCKERHUB_USERNAME}" --password-stdin

        if [[ ! $? ]] ; then
        
            echo "Docker Hub login failed"
            return 1
        fi

    else
        echo "Push requires Docker Hub login."
        echo "However, your environment does not provide required authentication data."
        return 1
    fi

    ### push images into the builder repository
    if [[ "${FORCE_PUBLISHING_BUILDER_REPO:-0}" == "1" ]] ; then

        if [[ -n "${repo}" && "${repo}" != "${_prohibited_repo_name}" ]] ; then

            ### push all target tags into the builder repository
            for t in "${_deploy_builder_tags[@]}" ; do

                echo ; echo "Pushing builder image ${DOCKER_REPO}:${t}"

                if [[ "${t}" == "${keeper_repo_tag}" ]] ; then

                    # first element is the master repo (a keeper)
                    docker push "${DOCKER_REPO}:${t}"
                    exit_code=$?

                    if [[ ${exit_code} -ne 0 ]] ; then
                        docker logout
                        die "Unable to push image '${DOCKER_REPO}:${t}'" ${exit_code}
                    fi
                else
                    target="${DOCKER_REPO}:${t}"

                    docker tag "${DOCKER_REPO}:${keeper_repo_tag}" "${target}"
                    docker push "${target}"
                    exit_code=$?
                    docker rmi "${target}"

                    if [[ ${exit_code} -ne 0 ]] ; then
                        docker logout
                        die "Unable to push image '${target}'" ${exit_code}
                    fi
                fi
            done

        else
            echo "Skipping forced builder repo push because of null or prohibited builder repository '${repo}'."
        fi
    fi

    ### push images into the deployment repository
    deployment_push

    ### Docker Hub log-out
    docker logout
}

main $@

echo -e "\n==> FINISHED  @$(date -u +'%Y-%m-%d_%H-%M-%S'): ${0} $@\n"
